package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;


public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设定请求与响应的编码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取请求参数
        String username = request.getParameter("username");
        System.out.println("username = " + username);
        String psw = request.getParameter("psw");
        System.out.println("psw = " + psw);
        String gender = request.getParameter("gender");
        System.out.println("gender = " + gender);
        //当根据参数的key（name属性）获取value时，如果对应的value可能为多个时
        String[] habits = request.getParameterValues("habit");
        System.out.println("habits = " + Arrays.toString(habits));
        //获取请求参数的name列表
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            System.out.println(names.nextElement());
        }
        //获取请求参数所有的k-v键值对
        Map<String, String[]> map = request.getParameterMap();
//        map.forEach((s, strings) ->  );
    }
}
